import requests
import re
import csv

resource = {
    '2017':'https://www.qschina.cn/sites/default/files/qs-rankings-data/cn/335219.txt?_=1589385261702',
    '2018':'https://www.qschina.cn/sites/default/files/qs-rankings-data/cn/379640.txt?_=1589385196689',
    '2019':'https://www.qschina.cn/sites/default/files/qs-rankings-data/cn/894194.txt',
    '2020':'https://www.qschina.cn/sites/default/files/qs-rankings-data/cn/2005378.txt'
}

# 使用正则表达式获得学校名称
def getTitle(content):
    pattern = re.compile('<a.*?>(.*?)<\\/a>')
    result = re.search(pattern, content)
    return result.group(1)

#写入csv首行
def writeheader():
    with open('ranking.csv', 'w', newline='',encoding='utf-8') as csvfile:
        fieldnames = ['name','value','date']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
    csvfile.close()

# 写入当年数据
def writeByYear(year,result):
    with open('ranking.csv', 'a', newline='',encoding='utf-8') as csvfile:
        fieldnames = ['name','value','date']
        writer = csv.DictWriter(csvfile,fieldnames)
        for item in result.json()['data']:
            writer.writerow({'name':getTitle(item['title']),'value':item['score'],'date':year})
    csvfile.close()

def main():
    writeheader()
    for year in resource:
        result = requests.get(resource[year])
        writeByYear(year,result)
 
if __name__ == "__main__":
    main()